<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Admin\Controller;

/**
 * Description of ChargeController
 *
 * @author gc
 */
class ChargeController extends AdminController
{

    public function index()
    {
        $type = I("type");
        $status = I("status");
        if (!empty($type)) {
            $map['type'] = $type;
        }
        if (strlen($status) > 0) {
            $map['status'] = array('eq', $status);
        } else {
            $map['status'] = array('egt', 0);
        }

        $list = $this->lists('Class_pay', $map, 'status asc,create_time desc');
        $this->assign('_list', $list);

        $totalfee = M("Class_pay")->where($map)->sum('fee');
        $this->assign('totalfee', $totalfee);

        $this->meta_title = '收费';
        //缴费方式
        $this->assign('_pay_method', C("PAY_METHOD"));
        $this->assign('_type', C("COURSE_TYPE"));

        //状态
        $this->assign('_status', [0 => '待缴费', 1 => '进行中', 2 => '已完成']);

        $this->display();
    }

    public function ajaxPay()
    {
        $data_id = I("data_id");
        $pay_method = I("pay_method");
        $sid = I("sid");
        $data = [
            'pay_method' => $pay_method,
            'pay_time' => time(),
            'status' => 1,
            'update_time' => time()
        ];
        $s = M("Class_pay")->where("id=" . $data_id)->save($data);

        M("Student")->where('id=' . $sid)->setField('status', 3); //已签约

        $this->ajaxReturn(['status' => $s ? 1 : 0]);
    }

    public function add()
    {

        $rid = I("rid"); //评估ID

        $data = M("Learn")->where("id=$rid")->find();

        $this->assign('rid', $rid);
        $this->assign('sid', $data['sid']); //学生ID
        $this->assign('course_id', $data['course_id']); //课程ID
        //课程信息
        $course = M("Course")->where("id=" . $data['course_id'])->find();
        $this->assign("course", $course);

        //课程计划
        $map['course_id'] = $data['course_id'];
        $map['status'] = 1;
        $plan = M("Course_plan")->where($map)->order("gorder asc ")->select();
        $this->assign("plan", $plan);

        //缴费方式
        $this->assign('_pay_method', C("PAY_METHOD"));
        //默认上课时间
        $this->assign('class_start_time', date("Y-m-d H:i"));

        $this->meta_title = '费用';
        $this->display();
    }

    public function ajaxFeeCount()
    {
        $class_hour = I("class_hour"); //课时数
        $class_start_id = I("class_start_id"); //开始上课编号
        $course_id = I("course_id"); //课程ID

        $myarray = explode('_', $class_start_id);

        $class_start_id_order = $myarray[1] - 1; //课程开始序号
        $class_start_id_order = $class_start_id_order > 0 ? $class_start_id_order : 0;

        $course = M("Course")->where("id=" . $course_id)->find();

        $map['course_id'] = $course['id'];
        $map['status'] = 1;
        $plan = M("Course_plan")->field("id")->where($map)->order("gorder asc ")->select();

        $all_hours = count($plan);
        $all_hours -= $class_start_id_order;

        $class_hour = $class_hour < 1 ? $all_hours : $class_hour; //应上课时
        $class_hour = $class_hour < $all_hours ? $class_hour : $all_hours; //能上课时

        $fee = round($class_hour * $course['fee'], 2);

        $this->ajaxReturn(['status' => 1, 'fee' => $fee, 'class_hour' => $class_hour]);
    }

    public function save()
    {
        $post = I("post.");
        $post['class_start_time'] = strtotime($post['class_start_time']);
        $post['create_time'] = $post['update_time'] = time();
        $post['admin_id'] = UID;

        $s = M("Class_pay")->add($post);

        $s ? $this->success("操作成功！", U("Charge/index")) : $this->error("操作失败！");
    }

    public function delete()
    {
        $ids = I("id");
        $map['id'] = array('in', $ids);

        $data = [
            'status' => -1,
            'update_time' => time()
        ];

        $s = M("Class_pay")->where($map)->save($data);
        $s ? $this->success("操作成功！", U("index")) : $this->error("操作失败！");
    }

}
